// =================================================================
// File: import.cpp
// Desc: Contains function for importing files from the host file system
// =================================================================

// =================================================================
// INCLUDES
// =================================================================

#include <string>
#include <fstream>
#include <iostream>
#include "FSManager.h"
#include "commands.h"
#include "Directory.h"
#include "File.h"
#include "Inode.h"

using namespace std;

// =================================================================
// FUNCTIONS
// =================================================================

// =================================================================
// Name: import
// Desc: Imports a file on the host machine and stores it in the DPS file system
// Args: string src_path - the path of the src file on the host machine
//	 string dest_path - the path to put the file on the DPS file system
//       FSManager* fsm - pointer to active FSManager object
// =================================================================
void import(string src_path, string dest_path, FSManager *fsm){
  long length;
  char* buffer;

  ifstream is;
  is.open(src_path.c_str(), ios::binary);

  // Get length of file
  is.seekg(0, ios::end);
  length = is.tellg();
  is.seekg(0, ios::beg);

  // allocate memory
  buffer = new char[length+1];

  // read data as a block
  is.read(buffer, length);
  buffer[length] = '\0';
  is.close();

  int fd = open(dest_path, "w", fsm);
  write(fd, buffer, fsm);
  close(fd, fsm);

  delete[] buffer;
  
  cout << "Import of " << src_path << " to " << dest_path << " is a success!" << endl;
}

